package org.redis.cluster.test.transport.mina;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.buffer.SimpleBufferAllocator;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IoSessionConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaServer {

	private int port;
	
	public MinaServer(int port) {
		this.port=port;
	}
	
	public void start() throws Exception {
		IoAcceptor acceptor=new NioSocketAcceptor(1);
		
		acceptor.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(new MinaEncoder(),new MinaDecoder()));
		
		acceptor.getFilterChain().addLast("threadPool",new ExecutorFilter(Executors.newFixedThreadPool(1)));
		
		IoSessionConfig sessionConfig=acceptor.getSessionConfig();
		sessionConfig.setReadBufferSize(1024*1024);
		
		IoBuffer.setUseDirectBuffer(false);
		IoBuffer.setAllocator(new SimpleBufferAllocator());
		
		acceptor.setHandler(new MinaHandler());
		acceptor.bind(new InetSocketAddress(port));
	}
}